<template>
	<div class="system-menu-dialog-container">
		<el-dialog :rules="rules" @close="closeDialog" :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备编号：" prop="number" :rules="rules.number" :label-width="200">
							<el-input :disabled="disabledFlag" size="default"  placeholder="设备编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="序列号：" prop="serial_number" :rules="rules.serial_number" :label-width="200">
							<el-input size="default"  placeholder="序列号" style="max-width: 400px" v-model="ruleForm.serial_number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备厂商：" prop="manufacturer_id" :rules="rules.manufacturer_id" :label-width="200">
							<el-select clearable filterable   placeholder="设备厂商" style="width: 400px;" v-model="ruleForm.manufacturer_id">
								<el-option v-for="item in selectData.ManData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备型号：" prop="model_id" :rules="rules.model_id" :label-width="200">
							<el-select clearable filterable   placeholder="设备型号" style="width: 400px;" v-model="ruleForm.model_id">
								<el-option v-for="item in selectData.GenData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装区域：" prop="region_id" :rules="rules.region_id" :label-width="200">
							<el-select clearable filterable  placeholder="安装区域" style="width: 400px;" v-model="ruleForm.region_id">
								<el-option v-for="item in selectData.regionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装道路：" prop="road_id" :rules="rules.road_id" :label-width="200">
							<el-select clearable filterable  placeholder="安装道路" style="width: 400px;" v-model="ruleForm.road_id">
								<el-option v-for="item in selectData.roadData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装路口：" prop="intersection_id" :label-width="200">
							<el-select clearable filterable  placeholder="安装路口" style="width: 400px;" v-model="ruleForm.intersection_id">
								<el-option v-for="item in selectData.intersectionData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="安装路段：" prop="location_id" :label-width="200">
							<el-select clearable filterable  placeholder="安装路段" style="width: 400px;" v-model="ruleForm.location_id">
								<el-option v-for="item in selectData.locationData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="监控杆：" prop="monitor_pole_id" :label-width="200" :rules="rules.monitor_pole_id">
							<el-select clearable filterable  placeholder="监控杆" style="width: 400px;" v-model="ruleForm.monitor_pole_id">
								<el-option v-for="item in selectData.monitorPoleData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="ip地址：" :label-width="200" prop="ip_address" >
							<el-input  size="default"  
							placeholder="ip地址" style="max-width: 400px" v-model="ruleForm.ip_address"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="子网掩码：" :label-width="200" prop="subnet_mask" >
							<el-input   size="default"  
							placeholder="子网掩码" style="max-width: 400px" v-model="ruleForm.subnet_mask "> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="网关：" :label-width="200" prop="gateway" >
							<el-input   size="default"  
							placeholder="网关" style="max-width: 400px" v-model="ruleForm.gateway "> </el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="MEC序列号：" prop="mec_rcu_id" :rules="rules.mec_rcu_id" :label-width="200">
							<el-input size="default"  placeholder="MEC序列号" style="max-width: 400px" v-model="ruleForm.mec_rcu_id"> </el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { defineAsyncComponent, reactive, onMounted, ref,watch,defineProps } from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {useBasicApi} from "/@/api/system/menu/index"
import {userApi} from "/@/api/system/user/index"
import {useRoleApi} from "/@/api/system/role/index"
import {deptApi} from "/@/api/system/dept/index"
import {dicApi} from "/@/api/system/dic/index"
import {lampCollectorsApi} from "/@/api/fun/collector/index"
import {MansAPI} from "/@/api/fun/manufacturer/index"
import {GenreAPI} from "/@/api/fun/genre/index"
const GenApi = GenreAPI()
const ManApi = MansAPI()
const lampApi = lampCollectorsApi()
const BasicApi = useBasicApi()
const useApi = userApi()
const roleApi = useRoleApi()
const depApi = deptApi()
const diApi = dicApi()
const props = defineProps(['tableData',"disabled"])
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const disabledFlag = ref<Boolean>(false)

// dx定义start

	interface RuleForm {
		number:string,
		serial_number:string,
		region_id:any,
		road_id:any,
		intersection_id:any,
		location_id:any,
		monitor_pole_id:string,
		manufacturer_id:string,
		mec_rcu_id:string,
		model_id:string,
	}
	const ruleForm = ref({
		number:"",
		serial_number:"",
		region_id:null,
		road_id:null,
		intersection_id:null,
		location_id:null,
		monitor_pole_id:null,
		manufacturer_id:"",
		network_status:"",
		mec_rcu_id:"",
		model_id:"",
		ip_address:"",
		subnet_mask:"",
		gateway:"",
		
		
	
	})
	const selectData = reactive({
		regionData:[],
		intersectionData:[],
		roadData:[],
		locationData:[],
		monitorPoleData:[],
		ManData:[],
		GenData:[]
	})
	const searchForm = ref({})
	const getSelectData = (async ()=>{
		searchForm.value.search = `status:1`
		let res = await BasicApi.BasicTableApi(searchForm.value);
		if(res.isSuccess){
			selectData.regionData = res.returnData
		}
		let intersectionRes = await useApi.userTableApi(searchForm.value);
		if(intersectionRes.isSuccess){
			selectData.intersectionData = intersectionRes.returnData
		}
		let roadRes = await roleApi.RoleTableApi(searchForm.value);
		if(roadRes.isSuccess){
			selectData.roadData = roadRes.returnData
		}
		let locationRes = await depApi.getDepList(searchForm.value);
		if(locationRes.isSuccess){
			selectData.locationData = locationRes.returnData
		}
		let monitorRes = await diApi.getDicList(searchForm.value);
		if(monitorRes.isSuccess){
			selectData.monitorPoleData = monitorRes.returnData
		}
		let ManRes = await ManApi.getTableList();
		if(ManRes.isSuccess){
			selectData.ManData = ManRes.returnData
		}
		let GenRes = await GenApi.getTableList();
		if(GenRes.isSuccess){
			selectData.GenData = GenRes.returnData
		}
	})
	watch(props,(newVal,oldVal)=>{
		ruleForm.value = newVal.tableData
		disabledFlag.value = newVal.disabled
		//console.log(ruleForm.value,8521)
		
	})
	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '设备编号必填', trigger: 'blur' },
		],
		serial_number:[
			{ required: true, message: '序列号必填', trigger: 'blur' },
		],
		region_id:[
			{ required: true, message: '安装区域必选', trigger: 'blur' },
		],
		road_id:[
			{ required: true, message: '安装道路必选', trigger: 'blur' },
		],
		manufacturer_id:[
			{ required: true, message: '设备厂商必填', trigger: 'blur' },
		],
		mec_rcu_id:[
			{ required: true, message: 'MEC序列号必填', trigger: 'blur' },
		],
		model_id:[
			{ required: true, message: '设备必填', trigger: 'blur' },
		],
		monitor_pole_id:[
			{ required: true, message: '监控杆必填', trigger: 'blur' },
		],
	})








// dx定义end
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});


// 打开弹窗
const openDialog = () => {
		state.dialog.title = '新增信号灯采集器';
		state.dialog.submitTxt = '新 增';
		state.dialog.isShowDialog = true;
};
const menuDialogFormRef = ref(null)
// 关闭弹窗
const closeDialog = () => {
	menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async() => {
		const res = await lampApi.addlampCollector(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('添加成功')
			closeDialog(); // 关闭弹窗
			emit('refresh');
		}else{
			ElMessage.error(res.returnMsg)
		}

});
// 页面加载时
onMounted(() => {
	getSelectData()
});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
